/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.widgets;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import javax.swing.JComponent;

/**
 *
 * @author dpatchett
 */
public class SlicedDays extends JComponent {

    private class Period {
        
        private int minutesFrom;
        private int minutesTo;

        public Period(int minutesFrom, int minutesTo) {
            this.minutesFrom = minutesFrom;
            this.minutesTo = minutesTo;
        }

        public int getMinutesFrom() {
            return minutesFrom;
        }

        public int getMinutesTo() {
            return minutesTo;
        }                
    }

    private final static int[] ORDERED_DAYS = new int[] {
        GregorianCalendar.MONDAY,
        GregorianCalendar.TUESDAY,
        GregorianCalendar.WEDNESDAY,
        GregorianCalendar.THURSDAY,
        GregorianCalendar.FRIDAY,
        GregorianCalendar.SATURDAY,
        GregorianCalendar.SUNDAY
    };
    
    private class SlicedDay extends ArrayList<Period> {};

    private Color colour = Color.BLUE;
    private SlicedDay[] periods = new SlicedDay[] { new SlicedDay(),
            new SlicedDay(), new SlicedDay(), new SlicedDay(),
            new SlicedDay(), new SlicedDay(), new SlicedDay()
    };

    public SlicedDays() {
        if (java.beans.Beans.isDesignTime()) {
            for (int index = 0; index < 7; index++) {
                periods[index].add(new Period(
                        (5 + index) * 60,
                        (8 + index) * 60));
                periods[index].add(new Period(
                        (10 + index) * 60,
                        (13 + index) * 60));
            }
        }
    }

    public Color getColour() {
        return colour;
    }

    public void setColour(Color colour) {
        this.colour = colour;
    }

    public void clearPeriods() {
        for (int day = 0; day < 7; day++) {
            periods[day].clear();
        }
    }

    public void addPeriod(int day, int minutesFrom, int minutesTo) {
        periods[day-1].add(new Period(minutesFrom, minutesTo));
    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D graphics = (Graphics2D)g;

        int left = getInsets().left;
        int top = getInsets().top;

        int right = getWidth() - getInsets().right;
        int bottom = getHeight() - getInsets().bottom;

        int width = right - left;
        int height = bottom - top;

        int legendWidth = 30;
        int spaceWidth = 5;

        double heightPerMinute = (double)height / (double)(24 * 60);

        int barWidth = (width - legendWidth) / 7 - spaceWidth;

        int offset = left + legendWidth + spaceWidth;
        
        // Draw legend
        
        for (int hour = 0; hour < 24; hour++) {
            
            double x = left + 5;
            double y = hour * 60 * heightPerMinute;

            graphics.drawString(
                    Integer.toString(hour),
                    (float)x, (float)y);
            
            x = left + legendWidth;
            
            graphics.setColor(Color.GREEN);            
            graphics.drawLine((int)x, (int)y, (int)(width - x), (int)y);
        }
        
        // Draw the actual clocked time

        for (int index = 0; index < 7; index++) {
            
            for (Period period : periods[index]) {

                int x = offset;

                int y1 = top + (int)Math.round(
                        period.getMinutesFrom() * heightPerMinute);
                        
                int y2 = top + (int)Math.round(
                        period.getMinutesTo() * heightPerMinute);

                int barHeight = y2 - y1;

                graphics.setColor(Color.BLUE);
                graphics.fillRect(x, y1, barWidth, barHeight);
            }
            
            offset += (barWidth + spaceWidth);
        }
    }
}
